package org.example.apples;

import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class AppleTest {
    List<Apple> apples;

    @Before
    public void before() {
        apples = Arrays.asList(
                new Apple("red", 10, "USA"),
                new Apple("green", 12, "China"),
                new Apple("red", 200, "China")
        );
    }

    /**
     * 4.要么是重(150克以上)的红苹果,要么是绿苹果
     */
//    @Test
//    public void test04() {
//        Predicate<Apple> green =apples ->  apples.stream().filter(Apples -> Apples.getColor().equals("grren")).collect(Collectors.toList());
//        apples.stream().filter(Apples -> Apples.getWeight() > 140).filter(Apples -> Apples.getColor().equals("red")).collect(Collectors.toList());
//    }

    public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(
                new Apple("red", 10, "USA"),
                new Apple("green", 12, "China"),
                new Apple("red", 200, "China")
        );

        //4要么重150克以上的红苹果，要么是绿苹果
        System.out.println("//////");
    apples.stream().filter((apple ->
            apple.getWeight()>150&&apple.getColor().equals("red")||apple.getColor().equals("green")))
            .forEach(System.out::println);
        System.out.println("/////");
        //根据苹果的重量筛选,根据苹果的颜色筛选
        List<Apple> weight = apples.stream().filter(Apple -> Apple.getWeight() > 1).collect(Collectors.toList());

        weight.forEach(Apple -> {
            System.out.println(Apple.toString());
        });

        List<Apple> color = apples.stream().filter(Apple -> Apple.getColor().equals("red")).collect(Collectors.toList());

        color.forEach(Apple -> {
            System.out.println(Apple.toString());
        });
        List<Apple> a = apples.stream().sorted(Comparator.comparing(Apple::getWeight).reversed()).sorted(Comparator.comparing(Apple::getCountry).reversed()).collect(Collectors.toList());
        a.forEach(Apple -> {
            System.out.println(Apple.toString());
        });
        //3. 一个苹果 既是红色，又重量大于150
        List<Apple> b = apples.stream().filter(Apple -> Apple.getColor().equals("red")).filter(Apple -> Apple.getWeight() > 150).collect(Collectors.toList());
        b.forEach(Apple -> {
            System.out.println(Apple.toString());
        });
    }
//    在按重量比较两个苹果之后,你可能想要按原产国排序

}
